package leaveSystem.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mysql.jdbc.PreparedStatement;

import leaveSystem.bizlogic.LeaveManager;
import leaveSystem.bizlogic.ViewLeaveManager;
import leaveSystem.model.EmpLeave;
import leaveSystem.model.Employee;
import leaveSystem.model.Leave;
import leaveSystem.utility.DateUtility;



/**
 * Servlet implementation class ViewPersonalLeaveHistoryServlet
 */
@WebServlet("/viewpersonalleave")
public class ViewPersonalLeaveHistoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewPersonalLeaveHistoryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		DateUtility date = new DateUtility();
		String ui = request.getParameter("device");
		String id = request.getParameter("userid");
		
		int userid=0;
		if(id!=null)
			userid = Integer.parseInt(id);
		
		if(ui==null)
		{
			//by browser
			doProcess(request,response);
		}else {			
			System.out.println("Do mobile processing");
			//byMobile;
					
			LeaveManager manager = new LeaveManager();
			List<Leave> rst = manager.getLeaveByEmpId(userid);
			Employee emp = manager.findEmployeeById(userid);
			
			response.setContentType("text/plain");
			PrintWriter out = response.getWriter();
			
			for(Leave l:rst){
				out.println(l.getLeaveId());				
				if(l.getLeaveTypeId()==1)
					{out.println("Annual Leave");}
				else if(l.getLeaveTypeId()==2)
					{out.println("Medical Leave");}
				else
					{out.println("Compensation Leave");}
				
				out.println(emp.getName());
				try {
					out.println(date.ConvertUtilToSql(l.getLeaveStartDate()));
					out.println(date.ConvertUtilToSql(l.getLeaveEndDate()));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				out.println(l.getLeaveReason());
				out.println(l.getStatus());
				out.println(l.getRemainingLeaves());//number of days taken
				try {
					out.println(date.ConvertUtilToSql(l.getCreatedOn()));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}//leave apply date
			}			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request,response);
	}

	private void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("This is ViewPersonalHistoryServlet");
		HttpSession session=request.getSession();
		Employee emp=(Employee) session.getAttribute("currentSessionUser");
		int empid=emp.getEmployeeId();
		
		
		ViewLeaveManager manager=new ViewLeaveManager();
		
		ArrayList<EmpLeave> oldLeaves = manager.getPersonalLeaveHistory("<=",empid);
		ArrayList<EmpLeave> newLeaves = manager.getPersonalLeaveHistory(">",empid);
		
		request.setAttribute("oldleaves", oldLeaves);
		request.setAttribute("newleaves", newLeaves);
		
		RequestDispatcher rd = request.getRequestDispatcher("ViewPersonalLeaveHistory.jsp");
		rd.forward(request, response);
		
	}

}
